package com.hsck.order.kds.base.common

import android.app.Activity
import android.content.Context
import android.content.pm.ActivityInfo
import android.view.KeyEvent
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import coil.request.Disposable


abstract class BaseKtActivity : AppCompatActivity() {

    val disposableList = arrayListOf<Disposable>()

    override fun setContentView(layoutResID: Int) {
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) //隐藏状态栏
        super.setContentView(layoutResID)

        getIntentData()

        refreshData()
    }

    override fun onRestart() {
        super.onRestart()
        val token = getSharedPreferences("application", Context.MODE_PRIVATE)
            .getString("token", "")
        refreshData()
    }

    abstract fun initView()

    open fun refreshData() {

    }

    open fun getIntentData() {

    }

    override fun onDestroy() {
        super.onDestroy()

        try {
            if (disposableList.isNotEmpty()) {
                disposableList.forEach {
                    it.dispose()
                }
                disposableList.clear()
            }
        } catch (e: Exception) {

        }
    }

    override fun onStart() {
        super.onStart()
    }

    protected fun hideSoftKeyBoard(activity: Activity) {
        val inputMethodManager = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
        val v = activity.currentFocus
        if (v != null) {
            val isHide = inputMethodManager.hideSoftInputFromWindow(
                v.windowToken,
                InputMethodManager.HIDE_NOT_ALWAYS
            )

        }
    }

    /**
     * 隐藏软键盘(只适用于Activity，不适用于Fragment)
     */
    fun hideSoftKeyboard(editText: EditText) {
        val view = editText
        if (view != null) {
            val inputMethodManager =
                getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
            inputMethodManager.hideSoftInputFromWindow(
                view.windowToken,
                InputMethodManager.HIDE_NOT_ALWAYS
            )
        }
    }

    /**
     * 显示(只适用于Activity，不适用于Fragment)
     */
    fun showSoftKeyBoard(editText: EditText) {
        val view = editText
        if (view != null) {
            val inputMethodManager =
                getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
            inputMethodManager.hideSoftInputFromWindow(
                view.windowToken,
                InputMethodManager.SHOW_IMPLICIT
            )
        }
    }


    protected fun darkenBackground(bgcolor: Float) {
        val lp = this.window.attributes
        lp.alpha = bgcolor
        this.window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
        if (bgcolor == 1f) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND) //不移除该Flag的话,在有的页面上的会出现黑屏的bug
        } else {
            window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND) //此行代码主要是解决在华为手机上半透明效果无效的bug
        }
        window.setAttributes(lp)
    }

    override fun dispatchKeyEvent(event: KeyEvent): Boolean {
        if (event.keyCode != KeyEvent.KEYCODE_BACK && event.deviceId > 0) {
            return true;
        }
//        if (event.getDeviceId() > 0) {
//            ScanGunHelper.getInstance().analysisKeyEvent(event);
//            return true;
//        }

        return super.dispatchKeyEvent(event)
    }


}